﻿Imports NHibernate.Cfg
Imports NHibernate
Imports System.Diagnostics
Imports System.Collections
Public Class Formtest2
    Dim session As NHibernate.ISession
    'Dim addnew As Boolean
    'Dim thenew As NCS.muser

    Dim current As NCS.muser
    Dim currentname As String

    Private Sub MuserBindingSource_AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles MuserBindingSource.AddingNew
        log("add new")
        'addnew = True
        'thenew = New NCS.muser
        'e.NewObject = thenew
    End Sub

    Private Sub MuserBindingSource_BindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.BindingCompleteEventArgs) Handles MuserBindingSource.BindingComplete
        log("bind complete")
    End Sub

    Private Sub MuserBindingSource_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MuserBindingSource.CurrentChanged
        log("current change")
        If Not current Is Nothing Then
            log("current:" + currentname + "," + current.mname)
            If current.mname <> currentname Then
                log("name changed")
                If currentname = "" Then
                    log("new user:" + current.mname)
                    session.Save(current)
                    session.Flush()
                Else
                    log("can not change name")
                    current.mname = currentname
                End If
            Else
                If session.IsDirty Then
                    session.Flush()
                End If
            End If
        End If
        current = TryCast(Me.MuserBindingSource.Current, NCS.muser)
        currentname = current.mname
        log("current:" + current.mname)

    End Sub
    Sub log(ByVal s As String)
        Debug.Print(CStr(Now) + ":" + s)
    End Sub
    Private Sub MuserBindingSource_CurrentItemChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MuserBindingSource.CurrentItemChanged
        log("current item change")
        
    End Sub

    Private Sub MuserBindingSource_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.BindingManagerDataErrorEventArgs) Handles MuserBindingSource.DataError
        log("data error")
    End Sub

    Private Sub MuserBindingSource_DataMemberChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MuserBindingSource.DataMemberChanged
        log("data member change")
    End Sub

    Private Sub MuserBindingSource_DataSourceChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MuserBindingSource.DataSourceChanged
        log("data source change")
    End Sub

    Private Sub MuserBindingSource_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MuserBindingSource.Disposed
        log("dispose")
    End Sub

    Private Sub MuserBindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles MuserBindingSource.ListChanged
        'log("list change")
        'log(CStr(e.ListChangedType))
        'Select Case e.ListChangedType
        '    Case System.ComponentModel.ListChangedType.ItemDeleted
        '        log("list change:delete")
        '        log(CStr(e.NewIndex))
        '        log(CStr(e.OldIndex))
        '    Case System.ComponentModel.ListChangedType.ItemAdded
        '        log("list change:added")
        '    Case ComponentModel.ListChangedType.ItemChanged
        '        log("list change:item changed")
        '        If current.mname <> currentname Then
        '            log("name changed")
        '            If currentname = "" Then
        '                log("new user:" + current.mname)
        '                session.Save(current)
        '                session.Flush()
        '            Else
        '                log("can not change name")
        '                current.mname = currentname
        '            End If
        '        Else
        '            log("flush")
        '            session.Flush()
        '        End If
        '    Case ComponentModel.ListChangedType.ItemMoved
        '        log("list change:item moved")
        '    Case ComponentModel.ListChangedType.PropertyDescriptorAdded
        '        log("property added")
        '    Case ComponentModel.ListChangedType.PropertyDescriptorChanged
        '        log("prop change")
        '    Case ComponentModel.ListChangedType.PropertyDescriptorDeleted
        '        log("prop del")
        '    Case ComponentModel.ListChangedType.Reset
        '        log("reset")
        'End Select
    End Sub

    Private Sub MuserBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MuserBindingSource.PositionChanged
        log("position chaned")
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim c As Configuration = NCS.Class1.configuration
        Dim factory As ISessionFactory = c.BuildSessionFactory()
        'Dim firstId As String
        session = factory.OpenSession()
        Dim l As IList
        log("add.....")
        l = session.CreateCriteria(GetType(NCS.muser)).List()
        For Each i As NCS.muser In l
            Me.MuserBindingSource.Add(i)
        Next
        log("add finish")
        Me.MuserDataGridView.Columns(0).HeaderText = "编号"
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        If Not Me.MuserBindingSource.Current Is Nothing Then
            session.Delete(Me.MuserBindingSource.Current)
            log("delete")
            session.Flush()
        End If
    End Sub
End Class